package com.example.com.skills_utf8;

import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;




public class fragment41_0 extends Fragment{
	
	
	
	
	private PointF cur_point = new PointF();
	
	
    private Button btn_dest;
	private ImageView iv_dest;
	private TextView tv_display;

	private FrameLayout layout_dest;
	private FrameLayout.LayoutParams params;
	
	//
	
	
	float delta_x_left = 0, delta_x_right =0;
	float delta_y_top = 0, delta_y_bottom =0;
	
	
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = inflater.inflate(R.layout.fragment_41_0, null);	
		
		   btn_dest = (Button)view.findViewById(R.id.btn_dest);
	        iv_dest = (ImageView)view.findViewById(R.id.iv_dest);
	        tv_display = (TextView)view.findViewById(R.id.tv_display);
	      
	        
	        
	        iv_dest.setLongClickable( true ); // 使能出发ACTION.MOVE的操作
	        iv_dest.setOnTouchListener( new Touch(iv_dest, layout_dest) );
	        
	        
	        params = (FrameLayout.LayoutParams)iv_dest.getLayoutParams();
	        
	        
	        Button btn_left = (Button) view.findViewById(R.id.btn_left);
	        Button btn_right = (Button) view.findViewById(R.id.btn_right);
	        
	        btn_left.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				
					//delta_x_left +=1;
					delta_x_left=10;
					
					// params是个引用，所以值能被动态改变；
					params.leftMargin = params.leftMargin + (int)delta_x_left; 	
					params.topMargin = params.topMargin + (int)delta_y_top;
					params.rightMargin = params.rightMargin + (int)delta_x_right;
					params.bottomMargin = params.bottomMargin + (int)delta_y_bottom;
					iv_dest.setLayoutParams(params);
					
					System.out.println("leftMargin: "+delta_x_left);
					
				}
			});
	        btn_right.setOnClickListener( new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					//delta_x_left -=1;
					delta_x_left=-10;
					
					params.leftMargin = params.leftMargin + (int)delta_x_left; 	
					params.topMargin = params.topMargin + (int)delta_y_top;
					params.rightMargin = params.rightMargin + (int)delta_x_right;
					params.bottomMargin = params.bottomMargin + (int)delta_y_bottom;
					iv_dest.setLayoutParams(params);
					
					System.out.println("leftMargin: "+delta_x_left);
				}
			});
		
		
		return view;
	}
	

	
	
	
	
	
	
	class Touch implements OnTouchListener
	{
		private PointF cur = new PointF();
		private PointF down = new PointF();
		private ImageView iv_dest;
		private FrameLayout layout_dest;
		private FrameLayout.LayoutParams params ;
		
		
		public Touch(ImageView view, FrameLayout layout){
			iv_dest = view;
			layout_dest = layout;
	//		params = (FrameLayout.LayoutParams)layout.getLayoutParams();
			
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			
			float x = event.getRawX();
			float y = event.getRawY();
			
			tv_display.setText("x: "+ x +"     y: "+y);

			cur.set(x,y);
			
			
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					
					down.set(cur);
					
					
					Rect outRect = new Rect();
					iv_dest.getHitRect(outRect);
					System.out.println( outRect.toString() );
					
					float fromXDelta = outRect.centerX();
					float fromYDelta = outRect.centerY();
					
					/*  移动后控件式发生了变化，但是它的HitRect（）还在原处呢；
  					TranslateAnimation trans = new TranslateAnimation(
							fromXDelta, x, fromYDelta, y);
					trans.setFillAfter(true);
					iv_dest.startAnimation(trans);
					
					*/
					
					break;
					
				case MotionEvent.ACTION_UP:
					break;
					
				case MotionEvent.ACTION_MOVE:
					
					float delta_x = down.x - cur.x;
					float delta_y = down.y - cur.y;
					
					String result = "ACTION_MOVE    "; 
					result +="delta_x: "+ delta_x +"     delta_y: "+ delta_y;
					tv_display.setText( result );
					System.out.println( result );
					
					//
					params = (FrameLayout.LayoutParams)iv_dest.getLayoutParams();
					params.leftMargin = params.leftMargin + (int)delta_x; 
					params.topMargin = params.topMargin + (int)delta_y;
					params.rightMargin = params.rightMargin + (int)delta_x;
					params.bottomMargin = params.bottomMargin + (int)delta_y;
					
					iv_dest.setLayoutParams(params);
					
					down.set(cur);
					
					break;
				
			}
			
			
			return false;
		}
		
		
		
	}
	
	
	
}